package com.stock.demo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.RadioGroup;

import com.slidingmenu.lib.SlidingMenu;
import com.stock.demo.framework.fun.Functions;
import com.stock.demo.framework.ui.activity.BaseActivity;
import com.stock.demo.framework.ui.fragment.BaseFragment;
import com.stock.demo.framework.ui.fragment.FragmentFactory;
import com.stock.demo.framework.ui.view.BottomBar;
import com.stock.demo.function.market.view.MarketFragment;
import com.stock.demo.function.mine.view.MineFragment;
import com.stock.demo.function.selfselect.view.SelfSelectFragment;
import com.stock.demo.util.CommonUtils;
import com.stock.demo.util.SettingUtils;

import java.util.Calendar;

public class MainActivity extends BaseActivity {

    private long mTime;
    private BottomBar mBottomBar;
    private SlidingMenu mSlidingMenu;
    private int mPreviousCheck = -1;

    public static final String FUN_SLIDING_MENU_TOGGLE = "FUN_SLIDING_MENU_TOGGLE";

    public static void startAction(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        initSlidingMenu();
        initBottomBar();
    }

    private void initSlidingMenu() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        mSlidingMenu = new SlidingMenu(StockApplication.AppContext);
        mSlidingMenu.setMode(SlidingMenu.LEFT);
        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        // 设置压缩的效果
        mSlidingMenu.setBehindScrollScale(0.35f);
        // 设置滑动菜单拉开后的距离
        mSlidingMenu.setBehindOffset(CommonUtils.dp2px(100));
        // 设置阴影宽度
        mSlidingMenu.setShadowWidth(displayMetrics.widthPixels / 50);
        // 设置阴影的图片
        mSlidingMenu.setShadowDrawable(R.drawable.menu_shadow);
        // 设置是否开启渐变效果
        mSlidingMenu.setFadeEnabled(true);
        // 设置渐变的值范围(0.0-1.0f);
        mSlidingMenu.setFadeDegree(0.3f);
        mSlidingMenu.setBehindWidth((int) (displayMetrics.heightPixels / 2.3));
        mSlidingMenu.setMenu(R.layout.layout_menu_sliding);
        mSlidingMenu.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_sliding_menu, new MineFragment()).commit();
    }

    private void initBottomBar() {
        mBottomBar = (BottomBar) findViewById(R.id.bottom_bar_main);
        mBottomBar.setOnCheckedChangeListener(new BottomBar.OnCheckedChangeListener() {
            @Override
            public void onCheckedChange(RadioGroup group, int checkedId) {
                onBottomBarCheckChange(checkedId);
            }
        });
        mBottomBar.setCheck(0);
    }

    private void onBottomBarCheckChange(int checkedId) {
        BaseFragment baseFragment = null;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mPreviousCheck != -1) {
            transaction.hide(FragmentFactory.getFragment(mPreviousCheck));
        }
        switch (checkedId) {
            case R.id.rb_bottom_bar_item_0:
                baseFragment = FragmentFactory.getFragment(0);
                baseFragment.setFunctions(new Functions().addFunction(new Functions.Function(FUN_SLIDING_MENU_TOGGLE) {
                    @Override
                    public void function() {
                        mSlidingMenu.toggle();
                    }
                }));
                //第一次进入App会Check0，这里的逻辑是先不要初始化行情和自选界面
                if (mPreviousCheck != -1) {
                    setRefresh(false, false);
                }
                mPreviousCheck = 0;
                break;
            case R.id.rb_bottom_bar_item_1:
                baseFragment = FragmentFactory.getFragment(1);
                setRefresh(true, false);
                mPreviousCheck = 1;
                break;
            case R.id.rb_bottom_bar_item_2:
                baseFragment = FragmentFactory.getFragment(2);
                setRefresh(false, true);
                mPreviousCheck = 2;
                break;
            case R.id.rb_bottom_bar_item_3:
                baseFragment = FragmentFactory.getFragment(3);
                setRefresh(false, false);
                mPreviousCheck = 3;
                break;
        }
        if (baseFragment == null) {
            return;
        }
        if (baseFragment.getActivity() == null) {
            transaction.add(R.id.fl_fragment_container_main, baseFragment, String.valueOf(checkedId));
        }
        transaction.show(baseFragment).commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!SettingUtils.checkForceRefresh()) {//是否打开强制刷新
            if (mPreviousCheck == 1) {
                setRefresh(true, false);
            } else if (mPreviousCheck == 2) {
                setRefresh(false, true);
            } else {
                setRefresh(false, false);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        setRefresh(false, false);
    }

    @Override
    public void onBackPressed() {
        long time = Calendar.getInstance().getTime().getTime();
        if (time - mTime > 2000) {
            CommonUtils.showToast("再按一次退出");
            mTime = time;
        } else {
            finishAll();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        FragmentFactory.clearAll();
    }

    private void setRefresh(Boolean shouldMarketRefresh, boolean shouldSelfSelectRefresh) {
        if (!SettingUtils.checkForceRefresh()) {//是否打开强制刷新
            if (FragmentFactory.getFragment(1) instanceof MarketFragment) {
                ((MarketFragment) FragmentFactory.getFragment(1)).shouldRefresh(shouldMarketRefresh);
            }
            if (FragmentFactory.getFragment(2) instanceof SelfSelectFragment) {
                ((SelfSelectFragment) FragmentFactory.getFragment(2)).shouldRefresh(shouldSelfSelectRefresh);
            }
        }
    }
}
